package org.smartercontextmobile.mobileapp;

import org.smartercontextmobile.communication.Communication;
import org.smartercontextmobile.communication.ICommunication;
import org.smartercontext.contextmger.ciphermodule.CipherAES;
import org.smartercontext.contextmger.json.writer.IJSONWriter;
import org.smartercontext.contextmger.json.writer.JSONWriter;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class LoginActivity extends ActionBarActivity {
	
	public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_login,
					container, false);
			return rootView;
		}
	}
	
	/**
	 * This method allow to SmarterContext Mobile catches the event 
	 * generated when the login button is clicked.
	 * @param v - Represent the actual View on Android App
	 */
	public void login(View v){
		
		EditText textUsername = (EditText)findViewById(R.id.username_id);
		String usernameNonCipher = textUsername.getText().toString();
		CipherAES caes = new CipherAES();
		String usernameHex = caes.stringToHex(usernameNonCipher);
		String username = caes.encrypt_decrypt(usernameHex, true);
		
		EditText textPassword = (EditText)findViewById(R.id.password_id);
		String passwordNonCipher = textPassword.getText().toString();
		String passwordHex = caes.stringToHex(passwordNonCipher);
		String password = caes.encrypt_decrypt(passwordHex, true);
		
		IJSONWriter writer = new JSONWriter();
		String loginWrited = writer.writeLogin(username, password);
		
		ICommunication comm = new Communication();
		String auth=comm.loginSmarterContext(loginWrited);
				
		if(auth.equals("true")){
			
			Intent intent = new Intent(this,LocalizationActivity.class);
			intent.putExtra(EXTRA_MESSAGE, usernameNonCipher);
			startActivity(intent);
			finish();
			
		}else{
			TextView errorText = (TextView)findViewById(R.id.error_id);
			errorText.setText("Invalid credentials. Try Again!");
			errorText.setVisibility(View.VISIBLE);
		}
	}

}
